package com.xique.park.controller.client.web.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.park.controller.client.web.bean.domain.OpengateRecords;
import com.xique.park.controller.client.web.mapper.OpengateRecordsMapper;
import com.xique.park.controller.client.web.service.service.IOpengateRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 开关闸记录Service业务层处理
 *
 * @author xique
 * @date 2022-01-13
 */
@Service
public class OpengateRecordsServiceImpl extends ServiceImpl<OpengateRecordsMapper, OpengateRecords> implements IOpengateRecordsService {

    @Autowired
    private OpengateRecordsMapper opengateRecordsMapper;

    /**
     * 查询开关闸记录
     *
     * @param id 开关闸记录ID
     * @return 开关闸记录
     */
    @Override
    public OpengateRecords selectOpengateRecordsById(Long id) {
        return opengateRecordsMapper.selectById(id);
    }

    /**
     * 查询开关闸记录列表
     *
     * @param opengateRecords 开关闸记录
     * @return 开关闸记录
     */
    @Override
    public List<OpengateRecords> selectOpengateRecordsList(OpengateRecords opengateRecords) {
        QueryWrapper<OpengateRecords> wrapper = new QueryWrapper<>();
        return opengateRecordsMapper.selectList(wrapper);
    }

    /**
     * 新增开关闸记录
     *
     * @param opengateRecords 开关闸记录
     * @return 结果
     */
    @Override
    public int insertOpengateRecords(OpengateRecords opengateRecords) {
        return opengateRecordsMapper.insert(opengateRecords);
    }

    /**
     * 修改开关闸记录
     *
     * @param opengateRecords 开关闸记录
     * @return 结果
     */
    @Override
    public int updateOpengateRecords(OpengateRecords opengateRecords) {
        return opengateRecordsMapper.updateById(opengateRecords);
    }

    /**
     * 批量删除开关闸记录
     *
     * @param ids 需要删除的开关闸记录ID
     * @return 结果
     */
    @Override
    public int deleteOpengateRecordsByIds(List<Long> ids) {
        return opengateRecordsMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateSysUserId(List<OpengateRecords> list) {
        return opengateRecordsMapper.updateSysUserId(list);
    }

    @Override
    public int selectCountByClientUser(Long clientUserId, String beginTime, String endTime) {
        LambdaQueryWrapper<OpengateRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OpengateRecords::getParkId, SecurityUtils.getParkId());
        queryWrapper.eq(OpengateRecords::getOprUser, clientUserId).or().eq(OpengateRecords::getOprClientUser, clientUserId);
        queryWrapper.ge(StrUtil.isNotBlank(beginTime), OpengateRecords::getOprTime, beginTime);
        queryWrapper.le(StrUtil.isNotBlank(endTime), OpengateRecords::getOprTime, endTime);
        return count(queryWrapper);
    }

}
